//index.js
//获取应用实例
const app = getApp()
const api = require('../../utils/api.js')

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  onLoad: function () {
    var time = setInterval(
      function () {


        console.log('time time 1')

        var openid = wx.getStorageSync('openid')
        if (openid !== "") {
          clearInterval(time)
        }
      }, 400
    )
  },


  onShow: function () {

    var time = setInterval(
      function () {


        console.log('time time 1')

        var openid = wx.getStorageSync('openid')
        if (openid !== "") {
          clearInterval(time)


          api.get('isUserReged', {
            'openid': openid
          }).then(res3 => {
            console.log("res3: ", res3)
            if ('no' == res3.result) {
              console.log("NONONONOONONNOO")
              wx.redirectTo({
                url: '/pages/register/register',
              })
            } else {
              var userType = res3.userType
              var userPhone = res3.userPhone
              wx.setStorageSync('phone', userPhone)
              console.log("MYUSERTYPE: ", userType)
              if (userType == "manager") {
                wx.redirectTo({
                  url: '/pages/manager/manager'
                })
                // wx.setStorageSync('page', '/pages/manager/manager')
              } else {
                wx.redirectTo({
                  url: '/pages/worker/worker'
                })
                // wx.setStorageSync('page', '/pages/worker/worker')
              }
      
            }
          }).catch(e => {
            wx.showToast({
              title: 'register user fail',
            })
          })










          
        }
      }, 400
    )



  }
})